package com.ssd.job;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.alibaba.fastjson2.JSON;
import com.ssd.api.couponAndIntegral.PointsRecordApi;
import com.ssd.api.couponAndIntegral.dto.PointsRecordDTO;
import com.ssd.api.payment.dto.msg.TradeStatusMsg;
import com.ssd.common.VMSystem;
import com.ssd.entity.OrderEntity;
import com.ssd.mixly.service.ListenMqtt;
import com.ssd.service.OrderService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * 自定义消息处理类
 */
@Component
public class ListenMqttImpl implements ListenMqtt {

    @Resource
    private OrderService orderService;

    @Override
    public void listen(String topic, String message) {
        System.err.println("监听到支付消息");
        System.err.println("主题："+topic);
        System.err.println("消息："+message);
        TradeStatusMsg tradeStatusMsg= JSON.parseObject(message, TradeStatusMsg.class);
        //将需求封装成实体类
        OrderEntity orderEntity = new OrderEntity();
        //如果有交易单号则改为支付成功状态
        if(null != tradeStatusMsg.getProductOrderNo()){
            orderEntity.setStatus(VMSystem.ORDER_STATUS_PAYED);
            orderEntity.setPayStatus(VMSystem.PAY_STATUS_PAYED);
            orderEntity.setPayType(tradeStatusMsg.getTradingChannel());
            orderEntity.setTransactionId(tradeStatusMsg.getTransactionId());


            //如果有退款单号则改为支付成功状态
        }else if(null != tradeStatusMsg.getRefundNo()){
            orderEntity.setPayStatus(VMSystem.PAY_STATUS_REFUNDING);
            orderEntity.setRefundNo(tradeStatusMsg.getRefundNo());
        }
        orderEntity.setOrderNo(String.valueOf(tradeStatusMsg.getProductOrderNo()));
        orderEntity.setUpdateTime(LocalDateTime.now());
        orderService.changeStatus(orderEntity);
        System.out.println(tradeStatusMsg);
    }
}



